﻿@using NGroup.VTL.Models;
@using System.Collections.Generic;
@using NGroup.VTL.Core.Const;
@using NGroup.VTL.WebCore;
@model List<ProductModel>
@{
    ViewBag.Title = "Quản trị - Danh sách bài viết về sản phẩm";
    Layout = "~/Views/Shared/_AdminLayout.cshtml";
    PagingInfoModel pagingInfo = ViewBag.PagingInfo as PagingInfoModel;
    Dictionary<string, string> distinctAuthors = ViewBag.DistinctAuthors as Dictionary<string, string> ?? new Dictionary<string, string>();
    string currentFilterAuthor = Convert.ToString(ViewBag.currentFilterAuthor);
    string currentFilterProduct = Convert.ToString(ViewBag.currentFilterProduct);
    string currentFilterSupProduct = Convert.ToString(ViewBag.currentFilterSupProduct);
}
<div class="clear">
</div>
<!-- End .clear -->
<div class="content-box">
    @using (Html.BeginForm("danh_sach_san_pham", "vuarao", FormMethod.Post, new { id = "frmPaging" }))
    {
        <!-- Start Content Box -->
        <div class="content-box-header">
            <h3>
                Danh sách sản phẩm</h3>
            <div class="clear">
            </div>
            <div class="filterGroup">
                <select name="filter_author" id="filter_author" class="inputbox" size="1">
                    <option value="" selected="selected">- Người đăng -</option>
                    @foreach (var item in distinctAuthors)
                    {
                        if (currentFilterAuthor == item.Key)
                        {
                        <option value="@item.Key" selected="selected">@item.Value</option>
                        }
                        else
                        {
                        <option value="@item.Key">@item.Value</option>
                        }
                    }
                </select>
                <select name="filter_productSupType" id="filter_productSupType" class="inputbox"
                    size="1">
                    @foreach (var item in AppContentTypes.Level0ProductSelectListItems)
                    {
                        if (currentFilterSupProduct == item.Value)
                        {
                        <option value="@item.Value" selected="selected">@item.Text</option>                                    
                        }
                        else
                        {
                        <option value="@item.Value">@item.Text</option>       
                        }
                    }
                </select>
                <select name="filter_productType" id="filter_productType" class="inputbox" size="1">
                    @{
                    List<GenericTypeItem> subItems = new List<GenericTypeItem>();
                    subItems.Add(new GenericTypeItem { Description = "- Nhóm sản phẩm -" });

                    if (!string.IsNullOrWhiteSpace(currentFilterProduct))
                    {
                        subItems.AddRange(AppContentTypes.All.FindAll(e => e.SupTypeID.HasValue && e.TypeID.ToString() == currentFilterProduct));
                    }
                    else
                    {
                        subItems.AddRange(AppContentTypes.All.FindAll(e => e.SupTypeID.HasValue && e.SupTypeID.ToString() == currentFilterSupProduct));
                    }

                    foreach (var item in subItems)
                    {
                        if (item.TypeID.ToString() == currentFilterProduct)
                        {
                        <option value="@item.TypeID" selected="selected">@item.Description</option>
                        }
                        else
                        {
                        <option value="@item.TypeID">@item.Description</option>
                        }
                    }
                    }
                </select>
                <input type="submit" class="button" value="Lọc" />
            </div>
            <div class="clear">
            </div>
        </div>
        <div class="clear">
        </div>
        <!-- End .content-box-header -->
        <div class="content-box-content">
            <div class="tab-content default-tab" id="tab1">
                @if (Model != null && Model.Count > 0)
                {
                    <table>
                        <thead>
                            <tr>
                                <th>
                                    Tên bài viết
                                </th>
                                <th>
                                    Người đăng
                                </th>
                                <th>
                                    Ngày đăng
                                </th>
                                <th>
                                    Loại tin
                                </th>
                                <th>
                                    Công cụ
                                </th>
                            </tr>
                        </thead>
                        <tfoot>
                            <tr>
                                <td colspan="5">
                                    @Html.Partial("PagingPartial", pagingInfo)
                                </td>
                            </tr>
                        </tfoot>
                        <tbody>
                            @foreach (ProductModel item in Model)
                            {
                                <tr>
                                    <td>
                                        <a href="/vuarao/sua-san-pham/@item.SupTypeIDString/@item.TypeIDString/@item.UniqueString" title="@item.Title">@item.Title</a>
                                    </td>
                                    <td>
                                        @if (!string.IsNullOrWhiteSpace(item.CreatedBy))
                                        {
                                            <span>@item.CreatedBy.ToString()</span>
                                        }
                                        else
                                        {
                                            <span>-</span>
                                        }
                                    </td>
                                    <td>
                                        @{
                                        var createdAt = item.CreatedAt.ToString("dd-MM-yyyy");

                                        if (!string.IsNullOrWhiteSpace(createdAt))
                                        {
                                            <span>@createdAt</span>
                                        }
                                        else
                                        {
                                            <span>-</span>
                                        }
                                        }
                                    </td>
                                    <td>
                                        @AppContentTypes.All.Find(e => !e.SupTypeID.HasValue && e.TypeID == item.SupTypeID).Description
                                        -
                                        @AppContentTypes.All.Find(e => e.SupTypeID.HasValue && e.TypeID == item.TypeID).Description
                                    </td>
                                    <td>
                                        <!-- Icons -->
                                        <a href="/vuarao/sua-san-pham/@item.SupTypeIDString/@item.TypeIDString/@item.UniqueString" title="Edit">
                                            <img src="@Url.Content("~/Content/adminpages/images/icons/pencil.png")" alt="Edit" /></a>
                                        <a href="/vuarao/delete-product/@string.Format("{0}~{1}~{2}~{3}", item.ProductID, item.SupTypeID, item.TypeID, item.UniqueString)" class="btnDelete" title="Delete">
                                            <img src="@Url.Content("~/Content/adminpages/images/icons/cross.png")" alt="Delete" /></a>
                                    </td>
                                </tr>
                            }
                        </tbody>
                    </table>            
                }
                else
                {
                    <div style="padding: 20px;">
                        <p>
                            Không tìm thấy sản phẩm nào.</p>
                    </div>
                }
            </div>
        </div>
        <!-- End .content-box-content -->
    }
</div>
@section Lazy{
    <script type="text/javascript">
        $(".current").removeClass("current");
        $("#danh_sach_san_pham").addClass("current");
        $("#danh_sach_san_pham").parent("ul").css("display", "inherit");
        $("#qlsp").addClass("current");
        var __productsTypes = @Html.Raw(AppContentTypes.ProductSelectListItemsJson);

        $(document).ready(function(){
            $("#filter_productSupType").change(function () {
                UpdateProductFilterList();
            });
        });

        function UpdateProductFilterList()
        {
            var selectedID = $("#filter_productSupType option:selected").val();

            var subItems = __productsTypes[selectedID];
            $("#filter_productType").empty();
            $("#filter_productType").append("<option value=''>- Nhóm sản phẩm -</option>");

            if (!subItems == false && subItems.length > 0) {
                for (var i = 0; i < subItems.length; i++) {
                    var option = "<option value='" + subItems[i].Value + "'>" + subItems[i].Text + "</option>";
                    $("#filter_productType").append(option);      
                }           
            }
        }

    </script>
}
